package com.qf.service.impl;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dto.PageSearchDTO;
import com.qf.entity.MallGoodsBrand;
import com.qf.entity.MallGoodsSpec;
import com.qf.entity.MallGoodsSpecOptions;
import com.qf.entity.MallGoodsTemplate;
import com.qf.mapper.GoodsSpecMapper;
import com.qf.mapper.GoodsTemplateMapper;
import com.qf.service.IGoodsSpecService;
import com.qf.vo.PageResult;
import com.qf.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>title: com.qf.service.impl</p>
 * <p>Company: wendao</p>
 * author zhuximing
 * date 2021/9/16
 * description:
 */
@Service
@Transactional
public class GoodsSpecService implements IGoodsSpecService {
    @Autowired
    private GoodsSpecMapper goodsSpecMapper;
    @Autowired
    private GoodsTemplateMapper goodsTemplateMapper;

    @Override
    public PageResult<MallGoodsSpecOptions> selectSpecOptsByPage(PageSearchDTO<MallGoodsSpecOptions> optionsPageSearchDTO) {

        if (optionsPageSearchDTO == null) {
            return  new PageResult<>(false, "参数不合法");
        }

        //设置分页信息
        PageHelper.startPage(optionsPageSearchDTO.getPage(), optionsPageSearchDTO.getSize());

        List<MallGoodsSpecOptions> specOptsByPAge = goodsSpecMapper.findSpecOptsByPAge(optionsPageSearchDTO.getWhere());

        //获取分页信息
        PageInfo<MallGoodsSpecOptions> pageInfo = new PageInfo<>(specOptsByPAge);

        PageResult<MallGoodsSpecOptions> pageResult = new PageResult<>(true,"success");

        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setData(pageInfo.getList());

        return pageResult;

    }

    @Override
    public Result saveSpec(MallGoodsSpec spec) {

        if (spec == null) {
            return  new Result(false, "参数不合法");
        }

        if (spec.getOptions() == null || spec.getOptions().size()<=0) {
            return  new Result(false, "参数不合法");
        }

        //获取规格
        MallGoodsSpec specByName = goodsSpecMapper.findSpecByName(spec.getSpecName());
        if (specByName == null) {

            //保存规格信息
            goodsSpecMapper.saveSpec(spec);
            //保存规格选项
            spec.getOptions().forEach(options -> {
                options.setSpecId(spec.getId());
                goodsSpecMapper.saveOpts(options);
            });
        }else{
            spec.getOptions().forEach(options -> {

                //判断规格
                MallGoodsSpecOptions optByName = goodsSpecMapper.findOptByName(options.getOptionName());
                if (optByName == null) {
                    options.setSpecId(specByName.getId());
                    goodsSpecMapper.saveOpts(options);
                }
            });
        }

        return new Result(true, "success");
    }

    @Override
    public Result editSpec(MallGoodsSpec spec) {
        if (spec == null) {
            return  new Result(false, "参数不合法");
        }

        if (spec.getOptions() == null || spec.getOptions().size()<=0) {
            return  new Result(false, "参数不合法");
        }

        //获取规格
        MallGoodsSpec specByName = goodsSpecMapper.findSpecByName(spec.getSpecName());
        System.out.println(specByName);
        //保存规格信息
            goodsSpecMapper.editSpec(spec);

        for (MallGoodsSpecOptions options : spec.getOptions()) {
            //判断规格
            MallGoodsSpecOptions optByName = goodsSpecMapper.findOptByName(options.getOptionName());
            System.out.println(optByName);
            if (optByName == null) {
                options.setSpecId(specByName.getId());
                goodsSpecMapper.editOpts(options);
            }
        }

        return new Result(true, "success");
    }

    @Override
    public Result delSpec(MallGoodsSpecOptions options) {

        if (options == null) {
            return  new Result(false, "失败");
        }

            int i = goodsSpecMapper.delOpts(options);
            if (i>0){
                return new Result(true, "success");
            }
            else {
                return new Result(false, "失败");
            }

        }
    @Override
    public List<MallGoodsSpec> findSpecAndOptsBytemplateId(String templateId) {
        if(StringUtils.isEmpty(templateId)){

            return  new ArrayList<>();
        }

        MallGoodsTemplate template = goodsTemplateMapper.findById(templateId);

        if (template == null) {
            return  new ArrayList<>();
        }
        //[{"id":1,"name":"内存"},{"id":2,"specName":"颜色"}]
        String spec = template.getSpec();
        //将json数组转化为JSON对象  JSONUtil糊涂工具包
        List<MallGoodsSpec> specList = JSONUtil.toList(spec, MallGoodsSpec.class);


        specList.forEach(item->{
            //获取规格选项信息
            List<MallGoodsSpecOptions> optionsList
                    = goodsSpecMapper.findOptsBySpecId(item.getId()+"");
            //组合属性，得到要求数据的格式
            item.setOptions(optionsList);

        });

        return specList;
    }
}